<?php

namespace app\adminabc\controller;

use app\adminabc\controller\Base;

class Node extends Base {
	public function index() {
		$Alllist = model ( 'Node' )->getAllNode ();
		$tree = new \CateTree ();
		$treeArr = $tree->tree ( $Alllist, 'title', 'id', 'pid' );
		$this->assign ( 'tree', $treeArr );
		return $this->fetch ( 'list-node' );
	}
	function add() {
		if ($this->request->isPost ()) {
			$data ['title'] = $this->request->param ('title');
			$data ['name'] = $this->request->param ('name');
			$data ['status'] = $this->request->param ('status');
			$data ['level'] = $this->request->param ('level');
			$data ['pid'] = $this->request->param ('pid');
			$data ['sort'] = $this->request->param ('sort');
			
			$log = model ( 'Node' )->add ( $data );
			if ($log > 0)
				$this->success ( '添加成功' );
				else
					$this->success ( '添加失败' );
		} else {
			
			$Alllist = model ( 'Node' )->getAllNode ();
			$tree = new \CateTree ();
			$treeArr = $tree->tree ( $Alllist, 'title', 'id', 'pid' );
			$this->assign ( 'tree', $treeArr );
			return $this->fetch( '' );
		}
	}
	function edit() {
		$id = $this->request->param ( 'id' );
		if ($this->request->isPost ()) {
			$data ['title'] = $this->request->param ('title');
			$data ['name'] = $this->request->param ('name');
			$data ['status'] = $this->request->param ('status');
			$data ['level'] = $this->request->param ('level');
			$data ['pid'] = $this->request->param ('pid');
			$data ['sort'] = $this->request->param ('sort');
			
			$log = model ( 'Node' )->edit ( $id, $data );
			if ($log > 0)
				$this->success ( '修改成功' );
				else
					$this->success ( '修改失败' );
		} else {
			$info = model ( 'Node' )->nodeInfo ( array ('id' => $id ) );
			$Alllist = model ( 'Node' )->getAllNode ();
			$tree = new \CateTree ();
			$treeArr = $tree->tree ( $Alllist, 'title', 'id', 'pid' );
			$this->assign ( 'info', $info );
			$this->assign ( 'tree', $treeArr );
			return $this->fetch( 'add' );
		}
	}
	function del() {
		$id = $this->request->param ( 'id' );
		$log1 = model ( 'Node' )->del ( array (['id','=',$id ]) );
		if ($log1 > 0) {
			$log2 = model ( 'Node' )->delAccess ( array ('node_id' => $id ) );
			echo json_encode ( array ('status' => 'yes','msg' => '删除成功' ) );
		} else
			echo json_encode ( array ('status' => 'no','msg' => '删除失败' ) );
	}
}